package com.diors.game.engine.arpg.scene;

import java.util.List;

import org.apache.log4j.Logger;

import com.diors.game.engine.arpg.npc.model.NpcBornModel;
import com.diors.game.engine.arpg.scene.entity.MapEntity;
import com.diors.game.engine.arpg.scene.entity.SceneEntity;
import com.diors.game.engine.arpg.scene.entity.ScreenEntity;
import com.diors.game.engine.arpg.scene.model.SceneLineModel;
import com.diors.game.engine.arpg.scene.model.SceneModel;

/**
 * 场景工厂
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013-10-26
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public class SceneFactory 
{
	
	static final Logger log = Logger.getLogger(SceneFactory.class);
	
	/**
	 * 创建创景
	 * @param adapter	场景适配器
	 * @return			场景实体
	 * @create	2013-10-26	darren.ouyang
	 */
	public static SceneEntity createScene (SceneAdapter adapter)
	{
		initAdapter(adapter);
		
		SceneModel model = adapter.model;
		SceneLineModel lineModel = adapter.lineModel;
		
		SceneEntity scene = new SceneEntity(model, lineModel);
		scene.setId(MapUtils.getInstance().createSceneID());
		scene.setDestroyTime(adapter.destroyTime);
		
		initMaps(adapter, scene);
		return scene;
	}
	
	/**
	 * 初始化场景分线
	 * @param scene
	 * @param lineModel
	 * @create	2013-10-23	darren.ouyang
	 */
	private static void initMaps (SceneAdapter adapter, SceneEntity scene)
	{	
		/* 创建多条分线 */
		SceneLineModel lineModel = scene.gainLineModel();
		for (int i=0; i<lineModel.getMaxLine(); i++)
			createMap(scene);
	}
	
	/**
	 * 创建场景分线
	 * @param scene		场景实体
	 * @param lineID	分线ID
	 * @create	2013-10-23	darren.ouyang
	 */
	public static void createMap (SceneEntity scene)
	{	
		MapEntity map = new MapEntity();
		int maxX = 0;// scene.gainSceneModel().getMaxX();
		int maxY = 0;//scene.getMaxY();
		
		List<Integer> screenIDs = ScreenUtils.getInstance().getScreenIDs(maxX, maxY);
		for (int screenID : screenIDs)
		{
			ScreenEntity screen = createScreen(scene, screenID);
			map.getScreans().put(screenID, screen);
		}
		
		//scene.getLines().add(map);
	}
	
	/**
	 * 创建屏
	 * @param scene
	 * @param screenID
	 * @return
	 * @create	2013-10-23	darren.ouyang
	 */
	private static ScreenEntity createScreen (SceneEntity scene, int screenID)
	{
		ScreenEntity screen = new ScreenEntity();
		return screen;
	}
	
	/**
	 * 初始化适配器
	 * @param adapter	适配器实体
	 * @create	2013-10-26	darren.ouyang
	 */
	private static void initAdapter (SceneAdapter adapter)
	{
		SceneModel model = adapter.model;
		
		/* 摧毁时间为0时设置默认摧毁时间为无限 */
		if (adapter.destroyTime == 0)
			adapter.destroyTime = Long.MAX_VALUE;
		
		/* 设置默认的分线Model */
		if (adapter.lineModel == null)
			adapter.lineModel = new SceneLineModel(model.getId());
		
		if (adapter.bornList == null)
			adapter.bornList = model.getBornModelList();
	}

	/**
	 * 场景适配器
	 * @author	darren.ouyang <ouyang.darren@gmail.com>
	 * @date	2013-10-26
	 * @version 1.0
	 * @copyright Copyright (c) 2013, darren.ouyang
	 */
	public static class SceneAdapter
	{
		/* 场景model */
		SceneModel model;

		/* 场景摧毁时间 */
		long destroyTime;
		
		/* 场景分线Model */
		SceneLineModel lineModel;
		
		/* NPC出生列表 */
		List<NpcBornModel> bornList;
		
		/* 奇葩的情况 */
		Object miracle;

		public void setModel(SceneModel model) {
			this.model = model;
		}

		public void setDestroyTime(long destroyTime) {
			this.destroyTime = destroyTime;
		}

		public void setLineModel(SceneLineModel lineModel) {
			this.lineModel = lineModel;
		}
		
		public void setMiracle(Object miracle) {
			this.miracle = miracle;
		}

		public void setBornList(List<NpcBornModel> bornList) {
			this.bornList = bornList;
		}
		
		
	}

}
